import scala.util.parsing.combinator._
import org.specs._
//import payroll._
//import payroll.Type2Money._

object TestParsersV1{
  def main(args:Array[String]){

    println("a.s.dsdfsdf".split('.').toList)


    val input =
          """paycheck for employee "Buck Trends"
             is salary for 2 weeks minus deductions for {
               federal income tax            is  25.  percent of gross,
               state income tax              is  5.   percent of gross,
               insurance premiums            are 500. in gross currency,
               retirement fund contributions are 10.  percent of gross
             }"""
      val p = new PayrollParserCombinatorsV1
      val pweeks = p.parseAll(p.weeksDays, "weeks")
      println( pweeks )
      val parseRes = p.parseAll(p.paycheck, input)
      println(parseRes)
      def log(r:Any, ind:Int){
        r match {
          case n : p.~[a,b] =>
             log(n._1, ind+4)
             log(n._2, ind+4)
          case x => println(" ".*(ind) + x)
        }

      }

      parseRes match {
        case p.Success(r,_) =>
        log(r,5)
//          r match {
//            case n : p.~[a,b] => println("aaa=%s   bbb=%s".format(n._1,n._2))
//          }
      }
      System.nanoTime
  }
}

class PayrollParserCombinatorsV1 extends JavaTokenParsers {

  def paycheck = empl ~ gross ~ deduct

  def empl = "paycheck" ~> "for" ~> "employee" ~> employeeName

  def gross = "is" ~> "salary" ~> "for" ~> duration

  def deduct = "minus" ~> "deductions" ~> "for" ~> "{" ~> deductItems  <~ "}"

  // stringLiteral provided by JavaTokenParsers
  def employeeName = stringLiteral

  // decimalNumber provided by JavaTokenParsers
  def duration = decimalNumber ~ weeksDays

  def weeksDays : Parser[String] = "weeks" | "week" | "days" | "day"

  def deductItems = repsep(deductItem, "," )

  def deductItem = deductKind ~> deductAmount

  def deductKind = tax | insurance | retirement

  def tax = fedState <~ "income" <~ "tax"

  def fedState = "federal" | "state"

  def insurance = "insurance" ~> "premiums"

  def retirement = "retirement" ~> "fund" ~> "contributions"

  def deductAmount = percentage | amount

  def percentage = toBe ~> doubleNumber <~ "percent" <~ "of" <~ "gross"

  def amount = toBe ~> doubleNumber <~ "in" <~ "gross" <~ "currency"

  def toBe = "is" | "are"

  // floatingPointNumber provided by JavaTokenParsers
  def doubleNumber = floatingPointNumber
}